﻿Imports FinanceiroDAL
Public Class CentroCustoBLL
    Dim objDAL As New CentroCustoDAL
    Public tipo As String
#Region "Variaveis de instância"
    Property subcodigo As Integer
    Property codgrupo As Integer
    Property subnome As String
#End Region
#Region "Construtor"
    Sub New()
    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(ByVal subcod As Integer, ByVal grupoCod As Integer, subNome As String)
        Me.subcodigo = subcod
        Me.codgrupo = grupoCod
        Me.subnome = subNome
    End Sub
#End Region
#Region "Carrega Propriedades" '(ByVal subcod As Integer, ByVal grupoCod As Integer, subNome As String)
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New CentroCustoDAL
            tab = objDAL.buscaralt(codigo)
            If (tab.Rows.Count > 0) Then
                Me.subcodigo = Integer.Parse(tab.Rows(0)("sgr_codigo").ToString)
                Me.codgrupo = tab.Rows(0)("gru_codigo").ToString
                Me.subnome = tab.Rows(0)("sgr_nome").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        Me.subcodigo = ""
        Me.codgrupo = ""
        Me.subnome = ""
    End Sub
#End Region
#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Boolean
        Dim tab As New DataTable
        Try
            'verifica se já existe  
            If (Trim(Me.subnome) <> "") Then
                If (objDAL.valida(Me.subcodigo, Me.subnome, Me.codgrupo)) Then
                    objDAL = New CentroCustoDAL
                    Me.subcodigo = objDAL.Inserir(Me.codgrupo, Me.subnome)
                Else : Throw New Exception("Centro Custo já cadastrado")
                End If
            Else : Throw New Exception("Favor preencher todos os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (Me.subcodigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New CentroCustoDAL
            If (Me.subnome <> "") Then
                If (objDAL.valida(Me.subcodigo, Me.subnome, Me.codgrupo)) Then
                    ret = objDAL.Alterar(Me.codgrupo, Me.subnome, Me.subcodigo)
                Else : Throw New Exception("Centro Custo já cadastrado")
                End If
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region
#Region "Buscar"
    Function buscar(ByVal descricao As String, filtro As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New CentroCustoDAL
            ret = objDAL.buscar(descricao, filtro)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function localiza() As DataTable
        Dim ret As DataTable
        Try
            objDAL = New CentroCustoDAL
            ret = objDAL.localiza()
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    'Function localiza(ByVal cod As Integer) As DataTable
    '    Dim ret As DataTable
    '    Try
    '        objDAL = New CentroCustoDAL
    '        ret = objDAL.localiza(cod)
    '    Catch ex As Exception
    '        Throw ex
    '    Finally
    '        objDAL = Nothing
    '    End Try
    '    Return ret
    'End Function
#End Region
#End Region
End Class
